﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0712502_0712504_DesignPatternGame
{
    public class UserControlledTankLevel3 : UserControlledTank
    {
        public UserControlledTankLevel3(UserControlledTank tank)
            : this(tank.Exp, tank.TankManager)
        {
        }

        public UserControlledTankLevel3(int exp, UserControlledTankManager tankManager)
        {
            _exp = exp;
            _tankManager = tankManager;
         
            tankBodyFactory = new TankBodyFactory();
            this.Position = tankManager.TankLevel.Position;

            this.DirectionMoving = tankManager.TankLevel.DirectionMoving;
            this.RotationAngle = tankManager.TankLevel.RotationAngle;
            this.Size = tankManager.TankLevel.Size;    
            tankTurretFactory = new TankTurretFactory();
            this.MoveState = tankManager.TankLevel.MoveState;

            this._bulletManager = tankManager.TankLevel._bulletManager;
            this.Life = tankManager.TankLevel.Life;
            TimeDelayToReload = 6;
            ReloadTime = 0;            
        }

        public override void Initialize()
        {
            _lowerExp = 400;
            _upperExp = 600;

            tankTurretFactory.InitPrototypes();
            tankBodyFactory.InitPrototypes();

            AddBody((TankBodyModel)tankBodyFactory.CreateObject(2));
            AddTurret((TankTurretModel)tankTurretFactory.CreateObject(2));
            AddEngine(new GermanyEngine());

            this.Size = new Rectangle(0, 0, 54, 54);
            this.SizeOfRectInside = new Point(48, 48);
            this.SizeOfRectOutside = new Point(54, 54);

            this.ISpeed = this.EngineTank.Speed;
            this.Amor = this.BodyTank.Amor + this.TurretTank.Amor;
            this.Dammage = this.BodyTank.Damage + this.TurretTank.Dammage;
            this.MaxHP = 500;
            this.CurrentHP = MaxHP;
        }

        public override void AddExperiencePoint(int exp)
        {
            _exp += exp;
            LevelChangeCheck();
        }

        private void LevelChangeCheck()
        {
            //se xet tiep neu nhu nang cap game cap nhat them level4
        }

        public override void TankShot()
        {
            BulletLevel3 newBullet = new BulletLevel3();
            newBullet.IdTank = this.idTank;
            Vector2 direction = new Vector2(-(float)Math.Sin(-this.TurretRotationAngle), -(float)Math.Cos(-this.TurretRotationAngle));
            newBullet.Position = this.TurretPosition + new Vector2(27, 27) * direction;
            newBullet.RotationAngle = this.TurretRotationAngle;
            newBullet.IdTank = this.idTank;

            newBullet.TotalDamage = newBullet.Damages + this.Dammage;

          //  newBullet.CollisionRect = new Rectangle((int)newBullet.Position.X, (int)newBullet.Position.Y, 10, 10);
            this._bulletManager._listBullets.Add(newBullet);

            GameResourceContainer.SoundManager.DestBrick.Play();
        }
    }
}
